package com.moo2u2.android.oaktononthego.crm;

import java.io.File;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;

import com.moo2u2.android.oaktononthego.R;

public class ImageCursorAdapter extends SimpleCursorAdapter {

	Context ctx;

	public ImageCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		ctx = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = super.getView(position, convertView, parent);
		// TODO save image url in cursor
		Bitmap img = BitmapFactory.decodeFile(ctx.getFilesDir()
				.getAbsolutePath()
				+ File.separator
				+ CRMDetail.FILENAME
				+ (position + 1));
		Log.i(this.getClass().getSimpleName(), "Using image: "
				+ ctx.getFilesDir().getAbsolutePath() + File.separator
				+ CRMDetail.FILENAME + (position + 1));
		if (img != null)
			((ImageView) convertView.findViewById(R.id.profile_pic))
					.setImageBitmap(img);
		else
			Log.e(this.getClass().getSimpleName(), "Couldn't find image: "
					+ ctx.getFilesDir().getAbsolutePath() + File.separator
					+ CRMDetail.FILENAME + (position + 1));
		return convertView;
	}
}
